package com.ruoyi.ftrl.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ftrl.domain.TemperatureControl;
import com.ruoyi.ftrl.service.ITemperatureControlService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 温控阀管理Controller
 * 
 * @author dihang
 * @date 2024-08-08
 */
@RestController
@RequestMapping("/ftrl/control")
public class TemperatureControlController extends BaseController
{
    @Autowired
    private ITemperatureControlService temperatureControlService;

    /**
     * 查询温控阀管理列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:control:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemperatureControl temperatureControl)
    {
        startPage();
        List<TemperatureControl> list = temperatureControlService.selectTemperatureControlList(temperatureControl);
        return getDataTable(list);
    }

    /**
     * 查询采集器下的温控阀管理列表
     */
    @GetMapping("/lists/{id}")
    public TableDataInfo lists(@PathVariable("id") Long id)
    {
        startPage();
        List<TemperatureControl> list = temperatureControlService.selectTemperatureControlLists(id);
        return getDataTable(list);
    }

    /**
     * 导出温控阀管理列表
     */
    @PreAuthorize("@ss.hasPermi('ftrl:control:export')")
    @Log(title = "温控阀管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemperatureControl temperatureControl) throws UnsupportedEncodingException {
        List<TemperatureControl> list = temperatureControlService.selectTemperatureControlList(temperatureControl);
        // 设置导出文件名
        String fileName = "温控阀数据表.xlsx";
        // 获取当前时间并格式化
        LocalDateTime now = LocalDateTime.now();
        String timeStr = now.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        fileName = String.format("温控阀数据表_%s.xlsx", timeStr);


        // 处理文件名中文编码
        fileName = URLEncoder.encode(fileName, "UTF-8");

        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        ExcelUtil<TemperatureControl> util = new ExcelUtil<TemperatureControl>(TemperatureControl.class);
        util.exportExcel(response, list, "温控阀管理数据");
    }

    /**
     * 获取温控阀管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ftrl:control:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(temperatureControlService.selectTemperatureControlById(id));
    }

    /**
     * 新增温控阀管理
     */
    @PreAuthorize("@ss.hasPermi('ftrl:control:add')")
    @Log(title = "温控阀管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TemperatureControl temperatureControl)
    {
        return toAjax(temperatureControlService.insertTemperatureControl(temperatureControl));
    }

    /**
     * 修改温控阀管理
     */
    @PreAuthorize("@ss.hasPermi('ftrl:control:edit')")
    @Log(title = "温控阀管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TemperatureControl temperatureControl)
    {
        return toAjax(temperatureControlService.updateTemperatureControl(temperatureControl));
    }

    /**
     * 删除温控阀管理
     */
    @PreAuthorize("@ss.hasPermi('ftrl:control:remove')")
    @Log(title = "温控阀管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(temperatureControlService.deleteTemperatureControlByIds(ids));
    }
}
